package servTask;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import page.PageState;
import task.Task;
import task.taskCheck;
import usr.Usr;

public class servTaskDelete extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	{	//delete the task by taskid, drive by "tasks.jsp"
		try {
			HttpSession sess = req.getSession();
			Usr usr = (Usr)sess.getAttribute("usr");
			if(usr == null)
			{
				req.setAttribute("error_cause", PageState.LOGIN_NOT_EXIST);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
				return;
			}
			
			String staskid = req.getParameter("taskid");
			if(staskid == null)
				resp.sendRedirect("u.html");
			int taskid = Integer.parseInt(staskid);
			if(taskCheck.CheckUsr(taskid, usr.getId()) == false)
				resp.sendRedirect("u.html");
			
			Task task = new Task();
			task.setId(taskid);
			task.update();
			task.delete();
			
			resp.sendRedirect("tasks.jsp");
		}
		catch (IOException | ServletException | SQLException e) {
			try {
				resp.sendRedirect("u.html");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
